package cn.life.business.tool.dao.impl;

import cn.life.common.entity.vo.Pagination;
import cn.life.common.support.jdbc.AbstractSupportDao;
import cn.life.business.tool.dao.ToolDao;
import cn.life.business.tool.entity.po.Tools;
import cn.life.business.tool.entity.vo.ToolsVo;
import org.springframework.stereotype.Repository;

/**
 * @author wangzhenyu
 * @Date 2018/7/6  15:09
 */
@Repository
public class ToolDaoImpl  extends AbstractSupportDao implements ToolDao {

    @Override
    public int addTools(Tools tools) {
        String sql = "INSERT INTO factory_tool(tool_name,tool_number,tool_desc,applicant_name,start_date,end_date," +
                "still_state,start_damage,end_damage,file_url,project_id,opr_user_id,opr_user_name,opr_date) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        return update(sql,tools.getToolName(),tools.getToolNumber(),
                tools.getToolDesc(),tools.getApplicantName(),tools.getStartDate(),tools.getEndDate(),
                tools.getStillState(),tools.getStartDamage(),tools.getEndDamage(),tools.getFileUrl()
                ,tools.getProjectId(),tools.getOprUserId(),tools.getOprUserName(),tools.getOprDate());
    }

    @Override
    public int deleteTools(long toolId) {
        String sql="delete from factory_tool where tool_id=?";
        return update(sql,toolId);
    }

    @Override
    public int updateTools(Tools tools) {
        String sql="update factory_tool set still_state=? ,end_damage=?,end_image = ? where tool_id=?";
        return update(sql,tools.getStillState(),tools.getEndDamage(),tools.getEndImage(),tools.getToolId());
    }

    @Override
    public int updateToolsState(Tools tools) {
        String sql= "update factory_tool set still_state=? where tool_id=?";
        return update(sql,tools.getStillState(),tools.getToolId());
    }

    @Override
    public Pagination<ToolsVo> getAllToolsInfo(ToolsVo toolsVo) {
        StringBuffer sql= new StringBuffer("select * from factory_tool where 1=1");
        if ((!"".equals(toolsVo.getToolName())&&toolsVo.getToolName()!=null)||(!"".equals(toolsVo.getApplicantName())&&toolsVo.getApplicantName()!=null)){
            sql.append(" and (tool_name like'%"+toolsVo.getToolName()+"%' or applicant_name like '%"+toolsVo.getApplicantName()+"%') ");
        }
        if (!"".equals(toolsVo.getBeginDate())&&toolsVo.getBeginDate()!=null){
            sql.append(" and  '" + getSqlDate(toolsVo.getBeginDate()) + "' <=start_date");
        }
        if (!"".equals(toolsVo.getOverDate()) && toolsVo.getOverDate()!=null){
            sql.append(" and  '" +  getEndDate(toolsVo.getOverDate()) + "' >=end_date");
        }

        if (toolsVo.getProjectId()!=null && !"".equals(toolsVo.getProjectId()) ){
            sql.append(" and  project_id =" + toolsVo.getProjectId() + " ");
        }
        sql.append("   ORDER BY opr_date DESC ");

        return queryForListByPageEx(sql.toString(),ToolsVo.class,toolsVo.getPageIndex(),toolsVo.getPageSize());
    }

    @Override
    public ToolsVo getTool(ToolsVo toolsVo) {
        String sql= "select * from factory_tool where tool_id=? ";
        return queryForObject(sql,ToolsVo.class,toolsVo.getToolId());
    }


}
